package show.me.the.money.service.core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public abstract class EventSupportService extends AOPService {
	
	private HashMap<String,List<ServiceListener>> listeners = new HashMap<String, List<ServiceListener>>();;
	
	public void on(String evName,ServiceListener sl){
		List<ServiceListener> ls = listeners.get(evName);
		if(ls == null){
			ls = new ArrayList<ServiceListener>();
			listeners.put(evName, ls);
		}
		ls.add(sl);
	}
	
	public void un(String evName,ServiceListener sl){
		List<ServiceListener> ls = listeners.get(evName);
		if(ls == null){
			return;
		}
		int i = ls.indexOf(sl);
		if(i < 0){
			return;
		}
		ls.remove(i);
	}
	
	public boolean fireEvent(String evName,HashMap<String,Object> message){
		List<ServiceListener> ls = listeners.get(evName);
		if(ls == null){
			return true;
		}
		boolean ret = true;
		for(ServiceListener l : ls){
			if(!l.onMessage(message)){
				ret = false;
			}
		}
		return ret;
	}

}
